package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.domain.dto.NjtUserRegisterDto;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.NjtUserRegisterService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class NjtUserRegisterServiceImpl implements NjtUserRegisterService {

    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;
    // 原有方法实现...
    @Override
    public List<NjtUserRegister> getNjtUserRegisterList(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister) {
//        因为数据库有相应追踪字段(修改者，修改时间)
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());// 使用若依官方工具返回获取当前登录用户名
        njtUserRegister.setUpdateTime(new Date());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 声明事务，确保数据一致性
    public int deleteNjtUserRegisterByIds(List<Long> ids) {
        return njtUserRegisterMapper.deleteNjtUserRegisterByIds(ids);
    }

    @Override
    public int registerUser(NjtUserRegisterDto njtUserRegisterDto) {
        return 0;
    }

    @Override
    public NjtUserRegister login(String mobilePhone, String password) {
        return null;
    }

    @Override
    public boolean register(String mobilePhone, String password) {
        return false;
    }

    @Override
    public int insertNjtUserRegister(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    @Override
    public int insertNjtUserRegister1(NjtUserRegister njtUserRegister) {
        // 设置创建时间和更新时间
        njtUserRegister.setCreateTime(new Date());
        njtUserRegister.setUpdateTime(new Date());
        
        // 设置创建人和更新人（如果使用了若依系统的用户体系）
        njtUserRegister.setCreateBy(SecurityUtils.getUsername());
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());
        
        // 设置默认值
        if (StringUtils.isEmpty(njtUserRegister.getUserStatus())) {
            njtUserRegister.setUserStatus("0"); // 默认正常状态
        }
        if (StringUtils.isEmpty(njtUserRegister.getDelFlag())) {
            njtUserRegister.setDelFlag("0"); // 默认未删除
        }
        if (StringUtils.isEmpty(njtUserRegister.getMemberFlag())) {
            njtUserRegister.setMemberFlag("0"); // 默认非会员
        }
        if (StringUtils.isEmpty(njtUserRegister.getPerfectFlag())) {
            njtUserRegister.setPerfectFlag("0"); // 默认信息未完善
        }
        if (StringUtils.isEmpty(njtUserRegister.getAuthFlag())) {
            njtUserRegister.setAuthFlag("0"); // 默认未认证
        }
        // 在 insertNjtUserRegister 方法中添加
        if (StringUtils.isEmpty(njtUserRegister.getUnifiedSocialCreditCode())) {
            njtUserRegister.setUnifiedSocialCreditCode(""); // 设置空字符串作为默认值
        }


        // 如果密码不为空，进行加密处理
        if (!StringUtils.isEmpty(njtUserRegister.getPassword())) {
            njtUserRegister.setPassword(SecurityUtils.encryptPassword(njtUserRegister.getPassword()));
        }
        
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }


}